
##
## See: https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html#variable:CMAKE_%3CLANG%3E_COMPILER_ID
##
function (get_lang_compiler_id LANG_COMPILER_ID LANG_ID)
    cmake_policy(SET CMP0054 NEW)
    set(LOCAL_COMPILER_ID)
    set(_IS_CXX)
    if ((LANG_ID STREQUAL "cxx") OR (LANG_ID STREQUAL "cpp") OR (LANG_ID STREQUAL "cc"))
        set(CMAKE_LANG_COMPILER_ID "${CMAKE_CXX_COMPILER_ID}")
        set(_IS_CXX YES)
    else()
        set(CMAKE_LANG_COMPILER_ID "${CMAKE_C_COMPILER_ID}")
        set(_IS_CXX NO)
    endif()

    if ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "GNU")
        set(LOCAL_COMPILER_ID "gcc")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "TinyCC")
        set(LOCAL_COMPILER_ID "tcc")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "clang")
        if (_IS_CXX)
            if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
                # Using clang with clang-cl front end, since CMake 3.15
                set(LOCAL_COMPILER_ID "clang-cl")
            elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
                # Using clang with regular front end
                set(LOCAL_COMPILER_ID "clang")
            endif()
        else()
            if (CMAKE_C_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
                # Using clang with clang-cl front end, since CMake 3.15
                set(LOCAL_COMPILER_ID "clang-cl")
            elseif (CMAKE_C_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
                # Using clang with regular front end
                set(LOCAL_COMPILER_ID "clang")
            endif()
        endif()
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "AppleClang")
        set(LOCAL_COMPILER_ID "apple-clang")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "Intel")
        set(LOCAL_COMPILER_ID "icc")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "IntelLLVM")
        set(LOCAL_COMPILER_ID "intel-llvm")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "ARMCC")
        set(LOCAL_COMPILER_ID "arm-gcc")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "ARMClang ")
        set(LOCAL_COMPILER_ID "arm-clang")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "NVIDIA ")
        set(LOCAL_COMPILER_ID "nVidia")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "NVHPC ")
        set(LOCAL_COMPILER_ID "NvHPC")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "SunPro")
        set(LOCAL_COMPILER_ID "sun-pro")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "OpenWatcom")
        set(LOCAL_COMPILER_ID "watcom")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "G95")
        set(LOCAL_COMPILER_ID "g95-fortran")
    elseif ("${CMAKE_LANG_COMPILER_ID}" STREQUAL "MSVC")
        set(LOCAL_COMPILER_ID "msvc")
    else()
        set(LOCAL_COMPILER_ID "${CMAKE_LANG_COMPILER_ID}")
    endif()

    # message(STATUS "get_lang_compiler_id(): LOCAL_COMPILER_ID: ${LOCAL_COMPILER_ID}")
    set(${LANG_COMPILER_ID} "${LOCAL_COMPILER_ID}" PARENT_SCOPE)
endfunction(get_lang_compiler_id)

function (GetCompilerToolset TOOLSET LANG_ID)
    # message(STATUS "LANG_ID = ${LANG_ID}")
    set(LANG_COMPILER_ID)
    get_lang_compiler_id(LANG_COMPILER_ID ${LANG_ID})
    if (JLANG_CMAKE_SHOW_DETAIL)
        message(STATUS "LANG_COMPILER_ID = ${LANG_COMPILER_ID}")
    endif(JLANG_CMAKE_SHOW_DETAIL)
    if (MSVC_IDE)
        if (MSVC60)
            set(${TOOLSET} "vc60" PARENT_SCOPE)
        elseif (MSVC70)
            set(${TOOLSET} "vc2003" PARENT_SCOPE)
        elseif (MSVC71)
            set(${TOOLSET} "vc2003" PARENT_SCOPE)
        elseif (MSVC80)
            set(${TOOLSET} "vc2005" PARENT_SCOPE)
        elseif (CMAKE_COMPILER_2005)
            set(${TOOLSET} "vc2005" PARENT_SCOPE)
        elseif (MSVC90)
            set(${TOOLSET} "vc2008" PARENT_SCOPE)
        elseif (MSVC10)
            set(${TOOLSET} "vc2010" PARENT_SCOPE)
        elseif (MSVC11)
            set(${TOOLSET} "vc2012" PARENT_SCOPE)
        elseif (MSVC12)
            set(${TOOLSET} "vc2013" PARENT_SCOPE)
        elseif (MSVC13)
            set(${TOOLSET} "vc2014" PARENT_SCOPE)
        elseif (MSVC14)
            set(${TOOLSET} "vc2015" PARENT_SCOPE)
        elseif (MSVC15)
            set(${TOOLSET} "vc2017" PARENT_SCOPE)
        elseif (MSVC16)
            set(${TOOLSET} "vc2019" PARENT_SCOPE)
        elseif (MSVC17)
            set(${TOOLSET} "vc2022" PARENT_SCOPE)
        else(MSVC60)
            if (NOT LANG_COMPILER_ID STREQUAL "")
                set(${TOOLSET} "${LANG_COMPILER_ID}" PARENT_SCOPE)
            else()
                set(${TOOLSET} "msvc" PARENT_SCOPE)
            endif()
        endif(MSVC60)
    elseif (MINGW)
        if (NOT LANG_COMPILER_ID STREQUAL "")
            set(${TOOLSET} "mingw_${LANG_COMPILER_ID}" PARENT_SCOPE)
        else()
            set(${TOOLSET} "mingw" PARENT_SCOPE)
        endif()
    elseif (CYGWIN)
        if (NOT LANG_COMPILER_ID STREQUAL "")
            set(${TOOLSET} "cygwin_${LANG_COMPILER_ID}" PARENT_SCOPE)
        else()
            set(${TOOLSET} "cygwin" PARENT_SCOPE)
        endif()
    elseif (MSYS)
        if (NOT LANG_COMPILER_ID STREQUAL "")
            set(${TOOLSET} "msys_${LANG_COMPILER_ID}" PARENT_SCOPE)
        else()
            set(${TOOLSET} "msys" PARENT_SCOPE)
        endif()
    elseif (XCODE)
        if (NOT XCODE_VERSION STREQUAL "")
            set(${TOOLSET} "xcode_${XCODE_VERSION}" PARENT_SCOPE)
        else()
            set(${TOOLSET} "xcode" PARENT_SCOPE)
        endif()
    elseif (BORLAND)
        if (NOT LANG_COMPILER_ID STREQUAL "")
            set(${TOOLSET} "borland_${LANG_COMPILER_ID}" PARENT_SCOPE)
        else()
            set(${TOOLSET} "borland" PARENT_SCOPE)
        endif()
    elseif (WATCOM)
        if (NOT LANG_COMPILER_ID STREQUAL "")
            set(${TOOLSET} "watcom_${LANG_COMPILER_ID}" PARENT_SCOPE)
        else()
            set(${TOOLSET} "watcom" PARENT_SCOPE)
        endif()
    elseif (UNIX)
        if (NOT LANG_COMPILER_ID STREQUAL "")
            set(${TOOLSET} "${LANG_COMPILER_ID}" PARENT_SCOPE)
        else()
            set(${TOOLSET} "unix" PARENT_SCOPE)
        endif()
    else(MSVC_IDE)
        if (NOT LANG_COMPILER_ID STREQUAL "")
            set(${TOOLSET} "${LANG_COMPILER_ID}" PARENT_SCOPE)
        else()
            set(${TOOLSET} "unknown" PARENT_SCOPE)
        endif()
    endif(MSVC_IDE)
endfunction(GetCompilerToolset)
